import axios, { type AxiosInstance, type AxiosRequestConfig, type AxiosResponse, type InternalAxiosRequestConfig } from 'axios'
import { ElMessage } from 'element-plus'
import {userStore} from "@/store/user";
//axios配置
const config = {
    baseURL: 'http://localhost:8089/',
    //baseURL: '/api',
    timeout: 10000,
    withCredentials:true

}
//定义返回值类型
export interface Result<T = any> {
    code: number,
    message: string,
    data: T
}

class Http {
    //anxios实例
    private instance: AxiosInstance
    //初始化
    constructor(config: AxiosRequestConfig) {
        this.instance = axios.create(config)
        //配置拦截器
        this.interceptors()

    }
    //请求发送或者返回做处理
    private interceptors() {
        //请求头携带token
        this.instance.interceptors.request.use(
            (config: InternalAxiosRequestConfig) => {
                let store = userStore()
                let token = store.getToken
                if (token && config) {
                    config.headers!['Authorization'] = token
                }
                return config;
            },
            (error: any) => {
                error.data = {}
                error.data.message = '网络错误,请稍后再试!'
                return Promise.reject(error.data.message);
            }
        );
        //返回结果处理
        this.instance.interceptors.response.use(
            (response: AxiosResponse) => {
                const store = userStore()
                if(response.data.code==600){
                    //如果token过期返回登录页面
                    store.setToken("")
                    store.setUserId("")
                    localStorage.clear()
                    location.reload();
                } else if (response.data.code == 200) {
                    return response.data;
                } else {
                    ElMessage.error(response.data.message || '接口错误!')
                    return Promise.reject(response.data.message || '接口错误!');
                }
            }, (error: any) => {
                console.log('进入错误');
                error.data = {}
                if (error && error.response) {
                    switch (error.response.status) {
                        case 400:
                            error.data.message = '请求错误!'
                            ElMessage.error(error.data.message)
                            break;
                        case 401:
                            error.data.message = '未授权，请重新登陆'
                            ElMessage.error(error.data.message)
                            break;
                        case 403:
                            error.data.message = '拒绝访问'
                            ElMessage.error(error.data.message)
                            break;
                        case 404:
                            error.data.message = '请求错误,未找到接口'
                            ElMessage.error(error.data.message)
                            break;
                        case 405:
                            error.data.message = '请求方法未允许'
                            ElMessage.error(error.data.message)
                            break;
                        case 408:
                            error.data.message = '请求超时'
                            ElMessage.error(error.data.message)
                            break;
                        case 500:
                            error.data.message = '后台接口出错'
                            ElMessage.error(error.data.message)
                            break;
                        case 501:
                            error.data.message = '网络未实现'
                            ElMessage.error(error.data.message)
                            break;
                        case 502:
                            error.data.message = '网络错误'
                            ElMessage.error(error.data.message)
                            break;
                        case 503:
                            error.data.message = '服务超时'
                            ElMessage.error(error.data.message)
                            break;
                        case 505:
                            error.data.message = 'http版本不支持该请求'
                            ElMessage.error(error.data.message)
                            break;

                        default:
                            error.data.message = `网络错误,状态码：${error.response.status}`
                            ElMessage.error(error.data.message)

                    }
                } else {
                    error.data.message = '连接到服务器失败'
                    ElMessage.error(error.data.message)
                }

                return Promise.reject(error);
            }

        )
    }
    //post请求
    post<T = Result>(url: string, data?: object): Promise<T> {
        return this.instance.post(url, data)
    }
    //get请求
    get<T = Result>(url: string, params?: object): Promise<T> {
        return this.instance.get(url, { params })
    }
    //put请求
    put<T = Result>(url: string, data?: object): Promise<T> {
        return this.instance.put(url, data)
    }
    //delete请求
    delete<T = Result>(url: string): Promise<T> {
        return this.instance.delete(url)
    }
    //图片上传
    upload<T = Result>(url: string, params?: object): Promise<T> {
        return this.instance.post(url, params, { headers: { 'Content-Type': 'multipart/form-data' } })
    }

}
export default new Http(config)

